package com.example.lianjuan.ui.activeity;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.bumptech.glide.Glide;
import com.example.lianjuan.Base.BaseActivity;
import com.example.lianjuan.Model.domain.TicketResult;
import com.example.lianjuan.Presenter.impl.impl.TicketPresenterImpl;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.PresenterManang;
import com.example.lianjuan.Utils.StringSizeUtils;
import com.example.lianjuan.Utils.ToastUtil;
import com.example.lianjuan.Utils.UtilsUrl;
import com.example.lianjuan.View.ITickPagerCallback;
import com.example.lianjuan.databinding.ActivityTicketBinding;

import java.util.List;

public class TicketActivity extends AppCompatActivity implements ITickPagerCallback {
    private ActivityTicketBinding binding;
    private TicketPresenterImpl mTicketPresenter;
    private State current = State.NONE;
    private boolean mhasTaoBaoApp;

    enum State {
        NONE, ERROR, LOADING, SUCCESS
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTicketBinding.inflate(getLayoutInflater());
        View rootView = binding.getRoot();
        setContentView(rootView);
        Presenter();
        mTicketPresenter.registerViewCallback(this);
        initEvent();

    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    protected void Presenter() {
        //获取有没有安装淘宝app
        mTicketPresenter = PresenterManang.getInstance().getTicketPresenter();
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);
            mhasTaoBaoApp = packageInfo != null;
        } catch (PackageManager.NameNotFoundException e) {
            mhasTaoBaoApp = false;
            e.printStackTrace();
        }
        binding.ticketBtn.setText(mhasTaoBaoApp ? "打开淘宝领卷" : "复制淘口令");
        binding.ticketBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //复制淘口令到粘贴板
                String ticket = binding.ticketCode.getText().toString().trim();//去掉空格
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("ticket_taobao", ticket);
                cm.setPrimaryClip(clipData);
                if (mhasTaoBaoApp) {
                    Intent intent = getPackageManager().getLaunchIntentForPackage("com.taobao.taobao");
                    startActivity(intent);
                } else {
                    ToastUtil.showToast("淘口令已复制");
                }
            }
        });
    }

    protected void initEvent() {
        binding.ticketBackPress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void visibilityState() {
        binding.ticketCoverRotate.setVisibility(current == State.LOADING ? View.VISIBLE : View.GONE);
        binding.ticketCoverError.setVisibility(current == State.ERROR ? View.VISIBLE : View.GONE);
        binding.ticketCover.setVisibility(current == State.SUCCESS ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onDestroy() {
        mTicketPresenter.unregisterViewCallback(this);
        super.onDestroy();
    }

    @Override
    public void onNetworkError() {

    }

    @Override
    public void onLoadEmpty() {

    }

    @Override
    public void onLoadNone() {

    }

    @Override
    public void onLoadLoading() {
        current = State.LOADING;
        visibilityState();
    }

    @Override
    public void onLoadError() {
        current = State.ERROR;
        visibilityState();
    }

    @Override
    public void onTicketLoaded(String cover, TicketResult result) {
        if (binding.ticketCover != null && !TextUtils.isEmpty(cover)) {
            Glide.with(this).load(UtilsUrl.getCoverPath(cover, 300)).into(binding.ticketCover);
        }
        //当数据正常则显示
        if (result != null && result.getData().getTbk_tpwd_create_response() != null) {
            String TicketModel = result.getData().getTbk_tpwd_create_response().getData().getModel();
            String ticket = StringSizeUtils.SizeSub(TicketModel);
            binding.ticketCode.setText(ticket);
        }
        //显示正常页面
        current = State.SUCCESS;
        visibilityState();
    }
}
